package com.springboot.husha.controller.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.husha.entity.Appuser;
import com.springboot.husha.entity.Fenggedashan;
import com.springboot.husha.entity.Yuyue;
import com.springboot.husha.qpage.QPage;
import com.springboot.husha.satoken.StpUserUtil;
import com.springboot.husha.service.IAppuserService;
import com.springboot.husha.service.IFenggedashanService;
import com.springboot.husha.service.IYuyueService;
import com.springboot.husha.util.DateUtils;
import com.springboot.husha.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预约表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-02-28
 */
@Controller
@RequestMapping("/api/front/yuyue")
public class AppYuyueController {

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/yuyue";

    /**
     * 业务层
     */
    @Autowired
    private IYuyueService service;

    @Autowired
    private IAppuserService appuserService;

    @Autowired
    private IFenggedashanService iFenggedashanService;

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }

    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(Yuyue obj) {
        if(!StpUserUtil.isLogin()){
            return Result.reTokenError("用户未登录");
        }
        obj.setYhid(StpUserUtil.getLoginIdAsInt());
        QueryWrapper<Yuyue> queryWrapper=new QueryWrapper<Yuyue>();
        if(!StringUtils.isEmpty(obj.getOrderid())){
            queryWrapper.like("orderid",obj.getOrderid());
        }
        if(!StringUtils.isEmpty(obj.getYhid())){
            queryWrapper.eq("yhid",obj.getYhid());
        }
        queryWrapper.orderByDesc("ctime");
        List<Yuyue> yuyueList=this.service.list(queryWrapper);
        return Result.reOk("查询成功",yuyueList);
    }


    /**
     * 预约保存
     * @return
     */
    @RequestMapping("/order")
    @ResponseBody
    public Result order(@RequestBody Yuyue obj) {
        if(!StpUserUtil.isLogin()){
            return Result.reTokenError("用户未登录");
        }
        //获取用户id
        Appuser appuser=this.appuserService.getById(StpUserUtil.getLoginIdAsInt());
        obj.setOrderid(DateUtils.identityId("OD"));
        obj.setCtime(new Date());
        obj.setYhid(appuser.getId());
        obj.setYhname(appuser.getAccount());
        Fenggedashan fenggedashan=this.iFenggedashanService.getById(obj.getFgid());
        obj.setFgid(fenggedashan.getId());
        obj.setFgname(fenggedashan.getName());
        this.service.save(obj);
        return Result.reOk("预约成功");
    }


}
